package com.baizhi.springboot.service.impl;

import com.baizhi.springboot.annotation.LogAnnotation;
import com.baizhi.springboot.dao.ArticleDao;
import com.baizhi.springboot.entity.Article;
import com.baizhi.springboot.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Override
    //@Transactional(readOnly = true)
    @LogAnnotation(content = "查询所有文章分页",type = "查询操作")
    public Map selectArticleByPage(Integer page, Integer limit) {
        Integer offset=(page-1)*limit;
        List<Article> articles = articleDao.selectArticleByPage(offset, limit);
        Integer count = articleDao.selectCount();
        /* "code": 0,
          "msg": "",
          "count": 1000,
          "data": [{}, {}]*/
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", articles);
        return map;
    }

    @Override
    @LogAnnotation(content = "添加文章",type = "添加操作")
    public void insertArticle(Article article) {
        articleDao.insertArticle(article);
    }

    @Override
    @LogAnnotation(content = "删除文章",type = "删除操作")
    public void deleteArticle(Integer id) {
        articleDao.deleteArticle(id);
    }

    @Override
    //@Transactional(readOnly = true)
    @LogAnnotation(content = "查询单个文章",type = "查询操作")
    public Article selectOneArticle(Integer id) {
        return articleDao.selectOneArticle(id);
    }

    @Override
    @LogAnnotation(content = "修改文章",type = "修改操作")
    public void updateArticle(Article article) {
        articleDao.updateArticle(article);
    }
}
